<script>
export default {
    data () {
        return {
            //action:"http://123.15.45.140:8085/file/uploads",
            action:"/file/uploads",
            formQueryCondition:{},
            editForm:{},
            editFormCheck:null,
            // 是否正在提交中
            loading: false,
            queryformLabelWidth:"75px",
            editformLabelWidth: '120px',
            editFormPosition: "right",
            modelName:"",
            tableData:[],
            tableHeight:this.$store.getters.appHeightNum - 176 + 33,
            // 分页对象
            pager: {
                curPage: 1,
                sizes: [20, 30, 50, 100, 200, 400],
                curSize: 20,
                total: 0
            },
            // 排序对象
            sort: {
                // key: "keyId",
                key: "lastUpdatedDate",
                dir: "Desc"
            },
            // 是否显示编辑面板
            isShowEditForm: false,
            // 当前编辑面板执行的命令
            curCommand: "SaveNew",
            canEditCode:false,
            //点击的按钮
            currentResCode: "",
            headersObj: {
                'Content-Type': 'application/json',
                'App-Type': this.getUserInfo().appType,
                'Language': this.getUserInfo().language,
                'Authorization': 'Bearer ' + this.getUserInfo().tokenCode,
                'X-AOHO-UserId': this.getUserInfo().keyID
              },
        }
    },
    methods:{
        // 分页容量变化
        sizeChange: function(val) {
            this.pager.curPage = 1;
            this.pager.curSize = val;
            this.queryTable();
        },
        // 分页按钮
        currentPageChange: function (val) {
            this.pager.curPage = val;
            this.queryTable();
        },
        // 生成网格的序号
        indexCreate: function(index) {
            return (this.pager.curPage - 1) * this.pager.curSize + (index + 1);
        },
        // 排序回调函数
        sortChange: function(sortObj) {
            // alert(JSON.stringify(sortObj));
            console.log(sortObj);
            this.pager.curPage = 1;
            this.sort.key = sortObj.column.sortBy;
            if (sortObj.order == "ascending") {
                this.sort.dir = "asc";
            } else {
                this.sort.dir = "desc";
            }
            this.queryTable();
        },
        // 注销按钮
        logoffRow: function(index, row, rescode) {
            this.currentResCode = (rescode && rescode!=='') ? rescode : '';
            var _this = this;
            this.$confirm(this.$i18n.t("message.confirmlogoffMessage"), 
                this.$i18n.t("message.confirmTitle"), {
                confirmButtonText: this.$i18n.t("message.yes"),
                cancelButtonText: this.$i18n.t("message.cancel"),
                type: "warning",
                callback: function (action, instance) {
                    if (action == "confirm") {
                        var commandBody = {  "keyId": row.keyId, "version": row.version, isEnable: "Invalid" };
                        // var commandBody = {
                        //     data: [{ keyID: row.keyID, ver: row.version }],
                        //     val: "Invalid",
                        //     count: 1
                        // };
                        _this.loading = true;
                        _this.procCommand("PassProc",//"PartialUpdateProc",
                                _this.modelName,
                                "N",
                                commandBody,
                                null,
                                _this.submitFormCallback);
                    }
                    
                }
            });
        },
        // 恢复按钮
        revertLogoffRow: function(index, row, rescode) {
            this.currentResCode = (rescode && rescode!=='') ? rescode : '';
            var _this = this;
            this.$confirm(this.$i18n.t("message.confirmNormalMessage"), 
                this.$i18n.t("message.confirmTitle"), {
                confirmButtonText: this.$i18n.t("message.yes"),
                cancelButtonText: this.$i18n.t("message.cancel"),
                type: "warning",
                callback: function (action, instance) {
                    if (action == "confirm") {
                        var commandBody = {  "keyId": row.keyId, "version": row.version, isEnable: "Available" };
                        // var commandBody = {
                        //     data: [{ keyID: row.keyID, ver: row.version }],
                        //     val: "Invalid",
                        //     count: 1
                        // };
                        _this.loading = true;
                        _this.procCommand("RecoveryProc",//"PartialUpdateProc",
                                _this.modelName,
                                "N",
                                commandBody,
                                null,
                                _this.submitFormCallback);
                    }
                    
                }
            });
        },
        // 删除按钮
        deleteRow:function(index, row, rescode){
            this.currentResCode = (rescode && rescode!=='') ? rescode : '';
            var _this = this;
            this.$confirm(this.$i18n.t("message.confirmDelMessage"), 
                this.$i18n.t("message.confirmTitle"), {
                confirmButtonText: this.$i18n.t("message.yes"),
                cancelButtonText: this.$i18n.t("message.cancel"),
                type: "warning",
                callback: function (action, instance) {
                    if (action == "confirm") {
                        console.log(_this);
                        console.log(row);
                        var commandBody = {"keyId": row.keyId, "version": row.version};
                        _this.loading = true;
                        _this.procCommand("DelProc",
                                _this.modelName,
                                "N",
                                commandBody,
                                null,
                                _this.submitFormCallback);
                    }
                    
                }
            });
        },
        // 编辑按钮单击事件
        edit: function(index, row, rescode){
            this.currentResCode = (rescode && rescode!=='') ? rescode : '';
            //dataAccess.queryDomainModelByKeyID(this, modelName, "Query", row.keyID, true, this.$options.methods.editCallback);
            this.queryModelByKeyID(this.modelName,row.keyId,"byId",this.editCallback);
        },
         // 导出按钮
        onExport: function(rescode){
            this.currentResCode = (rescode && rescode!=='') ? rescode : '';
        },
        // 查询按钮
        onQuery: function(rescode) {
            this.currentResCode = (rescode && rescode!=='') ? rescode : '';
            this.pager.curPage = 1;
            this.queryTable();
        },
        // 查询网格数据
        queryTable: function() {
            this.loading = true;
            console.log(this.formQueryCondition)
            this.queryModel(this.modelName,"Query",this.formQueryCondition,this.pager,this.sort,this.setTableData)
        },
        // 查询网格数据成功后的回调
        setTableData: function(isSuccess,json) {
            this.currentResCode = '';
            this.loading = false;
            try{
                if (isSuccess) {
                    //alert(JSON.stringify(json));
                    this.pager.total = json.total;
                    this.tableData = [];
                    for (var i = 0; i < json.list.length; i++) {
                        this.tableData.push(json.list[i]);
                    }
                    
                }
                else {
                    this.$message.error(json.msg);
                }
            }
            catch(e){
                alert(e);
            }
        },
        // 提交编辑面板数据
        submitForm: function submitForm(formName, rescode){
            this.currentResCode = (rescode && rescode!=='') ? rescode : '';
            var _this = this;
            this.setEditFormCheck();

            this.$refs[formName].validate(function (valid) {
                if (valid) {
                    _this.loading = true;
                    _this.procCommand(_this.curCommand,
                    _this.modelName,
                    "Default",
                    _this.editForm,
                    _this.editFormCheck,
                    _this.submitFormCallback);
                } else {
                    return false;
                }
            });
        },
          
        //编辑面板提交后调用，设置唯一性验证条件,改方法需各组件重写
        setEditFormCheck:function setEditFormCheck(){},
        // 执行命令后的回调函数
        submitFormCallback: function submitFormCallback(isSuccess,json) {
            this.currentResCode = '';
            this.loading = false;
            if(isSuccess){
                this.isShowEditForm = false;
                this.$message({
                    message: this.$i18n.t("message.successTip"),//resource.message.successTip,
                    type: 'success'
                });
                this.queryTable();
            }
            else
            {
                this.$message({
                                message: json.msg,
                                type: 'warning'
                                });
            }
        },


    },
    mounted: function mounted() {
        
    }
}

</script>